package com.hanry.listener;

import com.hanry.Main;
import com.hanry.command.CategoryBit;
import com.hanry.command.Command;
import com.hanry.command.CommandBit;
import com.hanry.component.FrontAndBackJoystickView;
import com.hanry.component.LeftAndRightJoystickView;
import com.hanry.component.OnFrontAndBackJoystickMoveListener;

public class FrontAndBackListener implements OnFrontAndBackJoystickMoveListener{
	private Main main = null;
	public FrontAndBackListener(){}
	public FrontAndBackListener(Main main) {
		this.main = main;
	}
	public void onValueChanged(int power, int direction) {
		Command command = null;
		if (power >= 3) {
			main.directionValueBit.setValueByte(power);
			//System.out.println("frontAndBackJoystick "+ power);
			if (direction == FrontAndBackJoystickView.FRONT) {
				if (main.lastLeftAndRightCommand == LeftAndRightJoystickView.LEFT) {
					command = new Command(new CategoryBit(
							CategoryBit.DIRECTION),
							new CommandBit(
									CommandBit.LEFT_FORWARD),
									main.directionValueBit);
				} else if (main.lastLeftAndRightCommand == LeftAndRightJoystickView.RIGHT) {
					command = new Command(new CategoryBit(
							CategoryBit.DIRECTION),
							new CommandBit(
									CommandBit.RIGHT_FORWARD),
									main.directionValueBit);
				} else {
					command = new Command(new CategoryBit(
							CategoryBit.DIRECTION),
							new CommandBit(CommandBit.FORWARD),
							main.directionValueBit);
				}
				main.sendCommand(command);
				main.lastFrontAndBackCommand = FrontAndBackJoystickView.FRONT;
			} else if (direction == FrontAndBackJoystickView.BACK) {
				if (main.lastLeftAndRightCommand == LeftAndRightJoystickView.LEFT) {
					command = new Command(
							new CategoryBit(
									CategoryBit.DIRECTION),
							new CommandBit(CommandBit.LEFT_BACK),
							main.directionValueBit);
				} else if (main.lastLeftAndRightCommand == LeftAndRightJoystickView.RIGHT) {
					command = new Command(new CategoryBit(
							CategoryBit.DIRECTION),
							new CommandBit(
									CommandBit.RIGHT_BACK),
									main.directionValueBit);
				} else {
					command = new Command(new CategoryBit(
							CategoryBit.DIRECTION),
							new CommandBit(CommandBit.BACK),
							main.directionValueBit);
				}
				main.sendCommand(command);
				main.lastFrontAndBackCommand = FrontAndBackJoystickView.BACK;
			}

		}
	}

	public void OnReleased() {
	}

	public void OnReturnedToCenter() {
		Command command = null;
		if (main.lastLeftAndRightCommand == LeftAndRightJoystickView.LEFT) {
			command = new Command(new CategoryBit(
					CategoryBit.DIRECTION), new CommandBit(
					CommandBit.LEFT), main.directionValueBit);
		} else if (main.lastLeftAndRightCommand == LeftAndRightJoystickView.RIGHT) {
			command = new Command(new CategoryBit(
					CategoryBit.DIRECTION), new CommandBit(
					CommandBit.RIGHT), main.directionValueBit);
		} else {
			command = new Command(new CategoryBit(
					CategoryBit.DIRECTION), new CommandBit(
					CommandBit.STOP), main.directionValueBit);
		}

		main.sendCommand(command);
		main.lastFrontAndBackCommand = FrontAndBackJoystickView.ORIGIN;
	}
}
